package com.raquel.greet;
import java.io.IOException;
import java.util.regex.Pattern;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet implementation class SingIn
 * Checks login, password and email format.
 * @author raquel_valenzuela
 */
@WebServlet("/SignIn")
public class SignIn extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public SignIn() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		PrintWriter output=response.getWriter();
		
		String login = request.getParameter("login");
		String dni = request.getParameter("dni");
		String email = request.getParameter("email");
		
		//validating values of login
		Pattern pattern = Pattern.compile("^[a-z]{8}$");
		java.util.regex.Matcher loginChecker = pattern.matcher(login);
		
		if(loginChecker.find()){
			output.write("Ok login format correct <br/>" + login + "<br/>");
		} else {
			//output.write("Login format incorrect <br/>" + login + "<br/>");
			this.getServletContext().getRequestDispatcher("/login.jsp").forward(request, response);
		}
		
		//validating values of dni
		pattern = Pattern.compile("^[0-9]{8}[A-Z]{1}$");
		loginChecker = pattern.matcher(dni);
		
		if(loginChecker.find()){
			output.write("Ok DNI format correct <br/>" + dni +"<br/>");
		} else {
			output.write("DNI format incorrect <br/>" + dni +"<br/>");
		}
		
		//validating values of email
		pattern = Pattern.compile("^[a-z0-9-_.]+@[a-z0-9-_.]+.[a-z]{1,6}$");
		loginChecker = pattern.matcher(email);
				
		if(loginChecker.find()){
			output.write("Ok email format correct <br/>" + email + "<br/>");
		} else {
			output.write("Email format incorrect <br/>"+ email + "<br/>");
		}
		
		output.write(login + " "+ dni+" " + email );
	}

}
